// 存储键名统一管理
const KEYS = {
  TODO: 'light_note_todo',
  DIARY: 'light_note_diary',
  TOMATO: 'light_note_tomato',
  USER_INFO: 'light_note_user_info',
  FRIENDS: 'light_note_friends',
  VIEW_PERMISSIONS: 'light_note_view_permissions',
  FRIEND_MESSAGES: 'light_note_friend_messages'
}

// 获取数据
export const getStorage = (key) => {
  const data = uni.getStorageSync(KEYS[key])
  return data ? JSON.parse(data) : []
}

// 保存数据
export const setStorage = (key, data) => {
  uni.setStorageSync(KEYS[key], JSON.stringify(data))
}

// 获取单个用户信息
export const getUserInfo = () => {
  const data = uni.getStorageSync(KEYS.USER_INFO)
  return data ? JSON.parse(data) : { userId: Date.now().toString(), nickname: '默认用户', avatar: '', moodPermissions: true }
}

// 保存用户信息
export const saveUserInfo = (userInfo) => {
  uni.setStorageSync(KEYS.USER_INFO, JSON.stringify(userInfo))
}

// 获取密友列表
export const getFriends = () => {
  const data = uni.getStorageSync(KEYS.FRIENDS)
  return data ? JSON.parse(data) : []
}

// 保存密友列表
export const saveFriends = (friends) => {
  uni.setStorageSync(KEYS.FRIENDS, JSON.stringify(friends))
}

// 获取查看权限设置
export const getViewPermissions = () => {
  const data = uni.getStorageSync(KEYS.VIEW_PERMISSIONS)
  return data ? JSON.parse(data) : { enabled: true, friends: [] }
}

// 保存查看权限设置
export const saveViewPermissions = (permissions) => {
  uni.setStorageSync(KEYS.VIEW_PERMISSIONS, JSON.stringify(permissions))
}

// 获取好友消息
export const getFriendMessages = () => {
  const data = uni.getStorageSync(KEYS.FRIEND_MESSAGES)
  return data ? JSON.parse(data) : []
}

// 保存好友消息
export const saveFriendMessages = (messages) => {
  uni.setStorageSync(KEYS.FRIEND_MESSAGES, JSON.stringify(messages))
}